package com.lagou.codec;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandler;
import io.netty.util.CharsetUtil;

/**
 * @description: 客户端自定义拦截器
 * @author: yx
 * @date: 2021/11/10 9:38
 */
public class NettyClientHandler implements ChannelInboundHandler {


    /**
     * 通道活跃事件
     *
     * @param ctx
     * @throws Exception
     */
    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        //ByteBuf byteBuf = Unpooled.copiedBuffer("你好,我是客户端", CharsetUtil.UTF_8);
        //ChannelFuture future = ctx.writeAndFlush(byteBuf);
        //不再需要使用ByteBuf传递,直接使用字符串
        ChannelFuture future = ctx.writeAndFlush("你好,我是客户端,解码后的");
        future.addListener(new ChannelFutureListener() {
            @Override
            public void operationComplete(ChannelFuture future) throws Exception {
                if (future.isSuccess()) {
                    System.out.println("客户端发送消息成功=======");
                }else {
                    System.out.println("客户端发送消息失败=======");
                }
                System.out.println("success: " + future.isSuccess());
                System.out.println("cause: " + future.cause());//获取已完成的当前操作失败的原因
                System.out.println("void: " + future.isVoid());
                System.out.println("done: " + future.isDone());//来判断当前操作是否完成
                System.out.println("cancelled: " + future.isCancelled());//来判断已完成的当前操作是否被取消
                System.out.println("cancellable: " + future.isCancellable());//来判断已完成的当前操作是否通过
                System.out.println("future: " + future.toString());
            }
        });
    }

    /**
     * 读取事件
     *
     * @param ctx
     * @param msg
     * @throws Exception
     */
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
        //ByteBuf buf = (ByteBuf) msg;
        //System.out.println("服务端发来的消息: " + buf.toString(CharsetUtil.UTF_8));
        //解码器进行了转换,可以直接进行打印
        System.out.println("服务端发来的消息: " + msg);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("读取事件完成============");
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {

    }


    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {

    }


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

    }

    @Override
    public void channelWritabilityChanged(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {

    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {

    }
}
